package com.qf.servlet;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/8/24 17:55
 * @Pacakge: com.qf.servlet
 * @ClassName: UserServlet
 * @Author: fan
 * 用户控制层
 */
@WebServlet("/findAllUser")
public class UserServlet extends HttpServlet {
    /**
     * javaweb常问面试题
     * servlet的生命周期
     * 1.用户请求的时候,会调用init()方法,进行初始化操作
     * 2.回去执行性service方法,根据不同的请求,执行业务流程
     * 3.服务器正常关闭的时候会调用destroy()方法,进行销毁
     *
     *
     */
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用业务接口查询所有用户
        List<User> list = userService.findAll();
        //将查询结果保存至request域对象中
        req.setAttribute("list",list);
        //设置返回头信息
        resp.setContentType("text/html;charset=utf-8");
        //请求转发
        req.getRequestDispatcher("/user.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
